package com.moon.controller;

import com.moon.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//定义Controller并声明Bean
@Controller
@RequestMapping("/user2")
public class UserController {
    //    响应页面/跳转页面
    @RequestMapping("/toJumpPage")
    public String toJumpPage() {
        System.out.println("跳转页面");
        return "page.jsp";
    }

    //    响应文本数据
    @RequestMapping("/toText")
    @ResponseBody
    public String toText() {
        System.out.println("返回纯文本数据");
        return "response text";
    }

    //    响应JSON对象
    @RequestMapping("/toJsonPOJO")
    @ResponseBody
    public User toJsonPOJO() {
        System.out.println("返回Json对象数据");
        User user = new User();
        user.setName("liu");
        user.setAge(22);
        return user;
    }

    //    响应POJO集合
    @RequestMapping("/toJsonList")
    @ResponseBody
    public List<User> toJsonList() {
        System.out.println("返回Json集合数据");
        User user = new User();
        user.setName("liu");
        user.setAge(22);

        User user2 = new User();
        user.setName("zhang");
        user.setAge(15);

        List<User> userList = new ArrayList<>();
        userList.add(user);
        userList.add(user2);

        return userList;
    }


}
